package cn.edu.ctbu.kcsj.webapi;

import cn.edu.ctbu.kcsj.core.PageUtils;
import cn.edu.ctbu.kcsj.domain.Teacher;
import cn.edu.ctbu.kcsj.service.TeacherService;
import cn.edu.ctbu.kcsj.utils.RUtil;
import cn.edu.ctbu.kcsj.vo.R;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

@RestController
@Slf4j
@RequestMapping("/webapi/teacher")
public class TeacherRestController {
    @Autowired
    private TeacherService teacherService;
    @GetMapping("/getbypage")
    public PageUtils getByPage(@RequestParam(value = "page",defaultValue = "0") Integer page,
                               @RequestParam(value = "size",defaultValue = "10") Integer size,
                               @RequestParam(value = "name",defaultValue = "") String name,
                               @RequestParam(value = "farm",defaultValue = "") String farm){
        Sort sort=Sort.by(Sort.Direction.ASC,"tno");
        Page<Teacher>teacherPage;
        if (StringUtils.isEmpty(name)){
            Pageable pageable=PageRequest.of(page,size,sort);
            teacherPage=teacherService.findAll(pageable);
        }else if (farm.equals("姓名")&&!StringUtils.isEmpty(name)){
            Teacher teacher=new Teacher();
            teacher.setTname(name);
            Pageable pageable=PageRequest.of(page,size,sort);
            ExampleMatcher matcher=ExampleMatcher.matching().withMatcher("tname",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Teacher>example=Example.of(teacher,matcher);
            teacherPage=teacherService.findAll(example,pageable);
        }else{
            Teacher teacher=new Teacher();
            teacher.setTno(Integer.parseInt(name));
            Pageable pageable=PageRequest.of(page,size,sort);
            ExampleMatcher matcher=ExampleMatcher.matching().withMatcher("tno",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Teacher>example=Example.of(teacher,matcher);
            teacherPage=teacherService.findAll(example,pageable);
        }
        PageUtils pageUtils=new PageUtils(teacherPage.getContent(),teacherPage.getTotalElements());
        return pageUtils;
    }
    @GetMapping("/getmy")
    public PageUtils getmy(HttpServletRequest httprequest){
        Page<Teacher>teacherPage;
        Sort sort=Sort.by(Sort.Direction.ASC,"tno");
        HttpSession session=httprequest.getSession();
        Integer tno=(Integer) session.getAttribute("userid");
        Teacher teacher=teacherService.getByTno(tno);
        teacher.setTno(teacher.getTno());
        Pageable pageable=PageRequest.of(0,1,sort);
        ExampleMatcher matcher=ExampleMatcher.matching().withMatcher("tno",ExampleMatcher.GenericPropertyMatchers.contains());
        Example<Teacher>example=Example.of(teacher,matcher);
        teacherPage=teacherService.findAll(example,pageable);
        PageUtils pageUtils=new PageUtils(teacherPage.getContent(),teacherPage.getTotalElements());
        return pageUtils;
    }
    /**
     * 新增老师方法 /webapi/teacher/insert
     * @param teacher
     * @return
     */
    @PostMapping("/insert")
    public R insert(@Valid Teacher teacher, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            String errormsg=bindingResult.getFieldError().getDefaultMessage();
            log.info("新增老师时出现错误！{}",errormsg);
            return RUtil.error(-10,errormsg);
        }
        teacherService.insert(teacher);
        return RUtil.success();
    }
    /**
     * 更新老师方法 /webapi/teacher/update
     * @param teacher
     * @return
     */
    @PutMapping("/update")
    public Teacher update(Teacher teacher){
        teacher=teacherService.update(teacher);
        return teacher;
    }
    /**
     * 删除老师方法 /webapi/teacher/delete
     * @param tno
     * @return
     */
    @DeleteMapping("/delete/{tno}")
    public void delete(@PathVariable Integer tno){
        teacherService.delete(tno);
    }
    @GetMapping("/get/{tno}")
    public Teacher get(@PathVariable Integer tno){
        Teacher teacher=teacherService.getByTno(tno);
        return teacher;
    }
    @RequestMapping ("/logout")
    public int logout(HttpServletRequest httprequest){
        HttpSession session=httprequest.getSession();
        session.setAttribute("userid",null);
        session.setAttribute("user",null);
        return 1;
    }
    @GetMapping("/get")
    public String get(HttpServletRequest httprequest){
        HttpSession session=httprequest.getSession();
        Integer tno=(Integer) session.getAttribute("userid");
        Teacher teacher=teacherService.getByTno(tno);
        String aa=teacher.getTpassword();
        return aa;
    }
    @GetMapping("/getinfo")
    public Teacher getinfo(HttpServletRequest httprequest){
        HttpSession session=httprequest.getSession();
        Integer tno=(Integer) session.getAttribute("userid");
        Teacher teacher=teacherService.getByTno(tno);
        return teacher;
    }
    @PutMapping("/save/{password}")
    public Integer save(HttpServletRequest httprequest,@PathVariable String password){
        HttpSession session=httprequest.getSession();
        Integer tno=(Integer) session.getAttribute("userid");
        Teacher teacher=teacherService.getByTno(tno);
        teacher.setTpassword(password);
        teacherService.update(teacher);
        return 1;
    }
}
